package com.hourupdate.network.rss;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;

import org.apache.log4j.Logger;

import com.hourupdate.network.bean.ChannelDetailNews;
import com.hourupdate.network.bean.ExecuteSQLBean;
import com.hourupdate.network.bean.RunningLock;
import com.hourupdate.network.connection.ConnectionJDBC;
import com.hourupdate.network.utils.Tools;

public class RssLoadData {
	static final Logger logger = Logger.getLogger(RssLoadData.class);
	public void loadData() {

		String sql = "";
		Connection conn = new ConnectionJDBC().getJDBConnection();
		ExecuteSQLBean ex = new ExecuteSQLBean();
		try {
			sql = "SELECT CN_ID, CN_NAME, CN_DESC, CN_LINK, CN_LINK_RSS, CN_GEN, CN_IMAGE, CN_STATUS, " +
				  "CN_CREATE_DATE, CN_MODIFE_DATE FROM "+ConnectionJDBC.schema+"."+ConnectionJDBC.schemaCHANNEL_NEWS+" WHERE CN_STATUS = ?";
			
			PreparedStatement ps = conn.prepareStatement(sql);
			ps.setString(1, "A");
			ArrayList<Map<String, String>> list = ex.executeQuery(ps);
			
			if(list != null && list.size() > 0){
				for(Map<String, String> data : list){
					RssLoadData rssLoadData = new RssLoadData();
					rssLoadData.getRssFeed(conn, data.get("CN_ID").toString(), data.get("CN_LINK_RSS").toString());
				}
			}
			list.clear();
			ps.close();
			conn.commit();
		} catch (SQLException e) {
			logger.error(e.getMessage());
		}finally{
			try {
				if(!conn.isClosed())conn.close();
			} catch (SQLException e) {
				logger.error(e.getMessage());
			}
		}
	}
	
	private void getRssFeed(Connection conn, String cn_id, String link){
		String cdn_id = "";
		String toDateTime = Tools.toDateTime();
		String curent_date = Tools.toDate();
		try{
			ChannelDetailNews channelDetailnews = null;
			RunningLock runningLock = null;
			RssFeedParser parser = new RssFeedParser(link,false);
			Feed feed = parser.readFeed();
			for (FeedMessage message : feed.getMessages()) {
				String pubdate = Tools.parseDate(message.getPubDate()).toString();
				String sql = "SELECT SEND_MASSAGE_TIME FROM "+ConnectionJDBC.schema+"."+ConnectionJDBC.schemaSEND_MASSAGE_TIME +
								" WHERE SEND_MASSAGE_TIME < ?";
				
				PreparedStatement ps = conn.prepareStatement(sql);
				ps = conn.prepareStatement(sql);
				ps.setString(1, pubdate);
				
				ExecuteSQLBean ex = new ExecuteSQLBean();
				ArrayList<Map<String, String>> list = ex.executeQuery(ps);
				ps.close();
				
				if(list != null && list.size() > 0){
					runningLock = RunningLock.getInstance();
					cdn_id = runningLock.getCdnId(cn_id, curent_date);
					
					channelDetailnews = new ChannelDetailNews();
					channelDetailnews.setCdn_id(cdn_id);
					channelDetailnews.setCn_id(cn_id);
					channelDetailnews.setCdn_create_date(toDateTime);
					channelDetailnews.setCdn_desc(message.getDescription());
					channelDetailnews.setCdn_image(message.getImage());
					channelDetailnews.setCdn_link(message.getLink());
					channelDetailnews.setCdn_title(message.getTitle());
					channelDetailnews.setCdn_publish_date(pubdate);
					channelDetailnews.insertPreparedStatement(conn);
				}
				list.clear();
			}
		}catch(Exception e){
			logger.error(e.getMessage());
		}
	}
}
